﻿var service = (function () {
    function callService(_fn, _rt, _uid, _tid, _did, _dt, _xd, _mod) {
        try {
            var servicePath = getServicePath(_fn), _r = undefined;
            var data = {};
            data.UserID = _uid;
            data.TaskGuid = _tid;
            data.DataGuid = _did;
            data.DataType = _dt;
            data.xmlTransform = _xd;

            $.ajax({
                type: _rt,
                url: servicePath,
                data: data,
                async: _mod,
                beforeSend: function () {
                    //com._cLog("beforeSend", _dt.toString(), "debug");
                },
                success: function (request, textStatus) {
                    _r = request.text;
                },
                error: function (request, textStatus) {
                    throw (request.responseText);
                }
            });

            return _r;

        } catch (e) {
            com._cLog("callService", e.description, "error");
        }
    }

    function getNewGuid() {

    }

    function getCurrentTime() {
        try {
            var _currentTime = soul.services.time;
            var _t = callService("getdata", "POST", "1", _currentTime.taskguid, _currentTime.dataguid, _currentTime.datatype, null, false);
            if (/Time+/g.test(_t)) {
                return com._cGetXmlObj(_t).find("Time").text();
            }
            else {
                throw ("data is error");
            }
        } catch (e) {
            com._cLog("getCurrentTime", e.description, "error");
        }
    }

    function getServicePath(_fn) {
        try {
            return soul.base + "PublicService/Communicate.asmx" + "/" + _fn;
        } catch (e) {
            com._cLog("getServicePath", e.description, "error");
        }
    }

    function easyGetData(_obj, _mod, _dg) {
        try {
            var _sObj = soul.services[_obj];
            var _sdg = _dg == undefined ? _sObj["dataguid"] : _dg;
            var _sRet = callService("GetData", "POST", soul.userid, _sObj["taskguid"], _sdg, _sObj["datatype"], null, _mod);
            if (!/Document+/g.test(_sRet)) {
                throw ("return data is error");
            }
            return _sRet;
        } catch (e) {
            com._cLog("easyGetData", e.description, "error");
        }
    }

    function easySetData(_obj, _mod) {
        try {
            var _sObj = soul.services[_obj];
            var _sResult = callService("SetData", "POST", soul.userid, _sObj["taskguid"], _sObj["dataguid"], _sObj["datatype"], _sObj["xmldata"], _mod);
        } catch (e) {
            com._cLog("easySetData", e.description, "error");
        }
    }

    function easyTransformData(_obj, _mod) {
        try {
            var _sObj = soul.services[_obj];
            return callService("TransformData", "POST", soul.userid, _sObj["taskguid"], null, null, _sObj["xmldata"], _mod);
        } catch (e) {
            com._cLog("getServicePath", e.description, "error");
        }
    }

    return {
        CallService: function (_fn, _rt, _uid, _tid, _did, _dt, _xd, _mod) {//sn:service name,fn:function name,rt:request type,uid:userID,tid:taskGuid,did:dataGuid,dt:dataType,xp:xmlData,mod:async
            return callService(_fn, _rt, _uid, _tid, _did, _dt, _xd, _mod);
        },
        GetCurrentTime: function () {
            return getCurrentTime();
        },
        EasyGetData: function (_obj, _mod, _dg) {
            return easyGetData(_obj, _mod, _dg);
        },
        EasySetData: function (_obj, _mod) {
            return easySetData(_obj, _mod);
        },
        EasyTransformData: function (_obj, _mod) {
            return easyTransformData(_obj, _mod);
        }
    }
})();